package com.game.object;

import android.graphics.Point;
import android.graphics.Rect;

import com.game.framwork.gamesupport.GameSprite;
import com.game.framwork.itf.Graphics;
import com.game.framwork.itf.Pixmap;

public class AnimateObject extends JObject {
	public final static int STAND = 0;
	public final static int MOVE_LEFT = 1;
	public final static int MOVE_RIGHT = 2;
	public final static int MOVE_TOP = 3;
	public final static int MOVE_BOTTOM = 4;

	protected Point velocity;
	public GameSprite[] animationList;
	private int numFrameInRow;
	private int currAnim;
	public Rect recSource = new Rect();
	protected int x;
	protected int y;

	public AnimateObject() {
		velocity = new Point();
		animationList = null;
		currAnim = 0;
		numFrameInRow = 1;
	}

	public void changeAnimation(String animationName) {
		int count = animationList.length;
		for (int i = 0; i < count; ++i) {
			if (animationList[i].getSpriteName() == animationName) {
				currAnim = i;
				return;
			}
		}
	}

	public void changeAnimation(int nextAnimIndex) {
		if (animationList == null)
			return;
		if (nextAnimIndex >= animationList.length)
			return;
		if (sprite == null)
			return;
		currAnim = nextAnimIndex;
	}

	public void loadSprite(Pixmap pImage, int numFrame, int numFrameInRow,
			int frameWidth, int frameHeight, GameSprite[] pAnimation) {
		setImage(pImage, frameWidth, frameHeight);
		this.numFrameInRow = numFrameInRow;
		animationList = pAnimation;
		changeAnimation(0);
	}

	public void loadSprite(Pixmap pImage, int numFrame, int numFrameInRow,
			int frameWidth, int frameHeight, GameSprite pAnimation) {
		setImage(pImage, frameWidth, frameHeight);
		this.numFrameInRow = numFrameInRow;
		animationList = new GameSprite[1];
		animationList[0] = pAnimation;
		changeAnimation(0);
	}

	@Override
	public void updateInternal(float deltaTime) {
		updateAnimation(deltaTime);
		super.updateInternal(deltaTime);
	}

	// / <summary>
	// / Update our animation
	// / </summary>
	public void updateAnimation(float deltaTime) {
		if (animationList != null) {
			animationList[currAnim].update(deltaTime, recSource, frameWidth,
					frameHeight, numFrameInRow);
		}
	}

	@Override
	public void paintInternal(Graphics g) {
		if (isVisible) {
			x = position.x - frameWidth / 2;
			y = position.y - frameHeight / 2;
			/*g.drawRect(getBoundBox().left, getBoundBox().top, getBoundBox()
					.width(), getBoundBox().height(), Color.WHITE);*/
			sprite.paint(g, new Point(x + tiledMap.getCamera().getCameraX(), y + tiledMap.getCamera().getCameraY()),
					recSource);
		}
		super.paintInternal(g);
	}

	public GameSprite getAnimation(int id) {
		int count = animationList.length;
		for (int i = 0; i < count; i++) {
			if (animationList[i].getSpriteId() == id) {
				return animationList[i];
			}
		}
		return null;
	}

	public GameSprite getAnimation(String name) {
		int count = animationList.length;
		for (int i = 0; i < count; i++) {
			if (animationList[i].getSpriteName() == name) {
				return animationList[i];
			}
		}
		return null;
	}

	public GameSprite getCurrAnimation() {
		return getAnimation(currAnim);
	}

	public boolean Collision(JObject pObj) {
		return super.Collision(pObj);
	}
}
